package hiultra.com.scollrecycle;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    private String[] titles1 = {"类型", "喜剧", "爱情", "动画", "剧情", "恐怖", "惊悚", "科幻", "动作", "悬疑", "冒险", "战争", "奇幻", "运动", "家庭", "古装", "武侠", "西部", "历史", "传记", "情色", "歌舞", "短片", "纪录片", "其他"};
    LinearLayout hscrollerview1;

    private RecyclerView mRecyclerView;
    private static List<String> mDatas;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.recy_recycle_laberFrag);
        hscrollerview1=findViewById(R.id.hscrollerview);
        setData2(hscrollerview1,titles1);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(new Adapter(MainActivity.this,initData()));
    }

    public static List<String> initData() {
        mDatas = new ArrayList<String>();
        for (int i = 'A'; i < 'z'; i++) {
            mDatas.add("" + (char) i);
        }
        return mDatas;
    }

    private void setData2(LinearLayout hscrollerview,String[] titles) {
        // ③ 再循环添加后面的数据
        for (int i = 0; i < titles.length; i++) {
            View view = View.inflate(MainActivity.this, R.layout.item_hscollerview0, null);
            TextView tv1 = (TextView) view.findViewById(R.id.tv0);
            //创建好TextView后就立马给它设置点击事件的监听
            setListener(tv1);
            tv1.setText(titles[i]);
            //添加进去
            hscrollerview.addView(view);
        }
    }

    private void setListener(final TextView tv1) {
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, tv1.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
